UserUnitGridPanel = function(limit){
	//结果集
	var store = new Ext.data.JsonStore(
		{
			url:__ctxPath+'/back/userunit/findallusers.html',
			totalProperty:'totalCount',
			root:'data',
			baseParams:{start:0,limit:limit},
			autoLoad:true,
			remoteSort:true,
			fields:['uid','areaname','address',
			'username','areaforbuild','areaforfees',
			'regdate','telephone','notes']
		}
	);
	//建立columnmodel 信息
	//修改一条记录信息
	var editOnlyOneInfo = function(grid, rowIndex, colIndex) {
        var rec = store.getAt(rowIndex);
        var win = new UserUnitWindow('修改信息');
		win.setIconClass('edit');
		win.show();
		win.load(rec);
		win.on('submit',function(win,form,data){
			form.getForm().submit(
				{
					url:__ctxPath+'/back/userunit/edituserunit.html',
					method:'POST',
					success:function(form,action){
						if(action.result.success){
							var d = rec.data;
							for(var i in d){
								rec.set(i,data[i]);
							}
							rec.commit();
							win.close();
							Ext.MessageBox.alert('系统提示','修改成功!');
						
						}else{
							Ext.MessageBox.alert('系统提示','修改失败!');
						}	
					},
					failure:function(form,action){
						Ext.MessageBox.alert('系统提示','网络原因 修改失败');
					}
				}
			);
		});
    }
	//删除一条记录信息
	var deleteOnlyOneInfo = function(grid, rowIndex, colIndex) {
        var rec = store.getAt(rowIndex);
        Ext.Msg.confirm('系统提示','是否确定删除选择的数据',function(btn){
			if(btn=='yes'){
				Ext.Ajax.request(
					{
						url:__ctxPath+'/back/userunit/deleteuserunit.html',
						method:'POST',
						params:{ids:rec.get('uid')},
						success:function(resp,option){
							var result = Ext.decode(resp.responseText);
							if(result.success){
								store.remove(rec);
								grid.getView().refresh();
								Ext.MessageBox.alert('系统提示','删除成功');	
							}else{
								Ext.MessageBox.alert('系统提示','删除失败');
							}
						},
						failure:function(resp,option){
							Ext.MessageBox.alert('系统提示','网络原因,删除失败');
						}
					}
				);
			}
		});
    }
    //SC选择框
	var sc = new Ext.grid.CheckboxSelectionModel();
	//列模型
	var cm = [
		new Ext.grid.RowNumberer(),
		sc,
		{
            xtype: 'actioncolumn',
            header:'操作',
            width: 50,
            items: [
            {
                iconCls :'edit',
                tooltip: '编辑用户',
                handler: editOnlyOneInfo
            }, {
                iconCls :'delete',
                tooltip: '删除用户',
                handler: deleteOnlyOneInfo
            }]
        },
		{header:'主键ID',dataIndex:'uid',sortable: true,width:200},
		{header:'小区名称',dataIndex:'areaname',sortable: true,width:200},
		{header:'住址',dataIndex:'address',sortable: true,width:200},
		{header:'用户名称',dataIndex:'username',sortable: true,width:200},
		{header:'建筑面积',dataIndex:'areaforbuild',sortable: true,width:200},
		{header:'采暖面积',dataIndex:'areaforfees',sortable: true,width:200},
		{header:'添加日期',dataIndex:'regdate',sortable: true,width:200},
		{header:'联系电话',dataIndex:'telephone',sortable: true,width:200},
		{header:'备注',dataIndex:'notes',sortable: true,width:200}
	];
	//新增信息
	var addInfo = function(){
		var me = this;
		var infowin = new UserUnitWindow('新增用户信息');
		infowin.setIconClass('add');
		infowin.show();
		infowin.on('submit',function(win,form,data){
			var areaname = form.getForm().findField('areaid').getRawValue();
			form.getForm().submit(
				{
					url:__ctxPath+'/back/userunit/adduserunit.html',
					method:'POST',
					success:function(form,action){
						if(action.result.success){
							var rec = new Ext.data.Record(data);
							rec.set('uid',action.result.uid);
							rec.set('areaname',areaname);
							rec.commit();
							store.insert(0,rec);
							Ext.MessageBox.alert('系统提示','添加成功!');
							win.close();
							me.getView().refresh();
						}else{
							Ext.MessageBox.alert('系统提示','添加失败!');
						}	
					},
					failure:function(form,action){
						Ext.MessageBox.alert('系统提示','网络原因 添加失败');
					}
				}
			);
		});
	};
	//修改信息
	var updateInfo = function(){
		var sel = this.getSelectionModel().getSelections();
			var win = new UserUnitWindow('修改信息');
			win.setIconClass('edit');
			if(sel.length!=1){
				Ext.MessageBox.alert('系统提示','只能选择一条数据对信息进行修改');
				return;
			}
			sel = this.getSelectionModel ().getSelected();
			win.show();
			win.load(sel);
			win.on('submit',function(win,form,data){
				form.getForm().submit(
					{
						url:__ctxPath+'/back/userunit/edituserunit.html',
						method:'POST',
						success:function(form,action){
							if(action.result.success){
								var d = sel.data;
								for(var i in d){
									sel.set(i,data[i]);
								}
								sel.commit();
								win.close();
								Ext.MessageBox.alert('系统提示','修改成功!');
							
							}else{
								Ext.MessageBox.alert('系统提示','修改失败!');
							}	
						},
						failure:function(form,action){
							Ext.MessageBox.alert('系统提示','网络原因 修改失败');
						}
					}
				);
			})
	};
	//删除信息
	var deleteInfo = function(){
		var me = this;
		var sel = this.getSelectionModel().getSelections();
		//得到record的数组信息
		var ids = [];
		for (var i = 0; i < sel.length; i++) {
			ids.push(sel[i].get('uid'));
		}
		Ext.Msg.confirm('系统提示','是否确定删除选择的数据',function(btn){
			if(btn=='yes'){
				Ext.Ajax.request(
					{
						url:__ctxPath+'/back/userunit/deleteuserunit.html',
						method:'POST',
						params:{ids:ids.join(',')},
						success:function(resp,option){
							var result = Ext.decode(resp.responseText);
							if(result.success){
								//服务器端成功删除后 在Store里面remove里面的数据
								for (var i = 0; i < sel.length; i++) {
									store.remove(sel[i]);
								}
								me.getView().refresh();
								Ext.MessageBox.alert('系统提示','删除成功');	
							}else{
								Ext.MessageBox.alert('系统提示','删除失败');
							}
						},
						failure:function(resp,option){
							Ext.MessageBox.alert('系统提示','网络原因,删除失败');
						}
					}
				);
			}
		});
		
	}
	//查询信息
	var queryInfo = function(){
		var win = new UserUnitWindow('信息查询');
		win.show();
		win.setIconClass('zoom');
		win.on('submit',function(win,form,data){
			var _data = data;
			for(var i in _data){
				if(_data[i]==null||_data[i]==''){
					delete _data[i];
				}
			}
			var _obj = {start:0,limit:limit};
			Ext.apply(_obj,_data);
			store.baseParams = _obj;
			store.reload();
			win.close();
		});
	}
	//用户导入
	var importUsers = function(){
		var obj = {title:'导入用户',iconCls:'excel-icon'};
		var win = new ImportUserWindow(obj);
		win.show();
		win.on('submit',function(win,form,data){
//			var _data = data;
			form.getForm().submit(
				{
					url:__ctxPath+'/back/userunit/importuserinfo.html',
					method:'POST',
//					params:{aaa:"aaa"},
					waitMsg: 'Uploading your photo...',
					success:function(form,action){
						alert('--249--');
					},
					failure:function(form,action){
						
					}
				}
			);
			
		});
	}
	var tbar = [
		{
			text:"新增信息",
			iconCls:"add",
			scope:this,
			handler:addInfo
		},'-',{
			text:'修改信息',
			iconCls:'edit',
			handler:updateInfo,
			scope:this
		},'-',
		{
			text:'删除信息',
			iconCls:'delete',
			scope:this,
			handler:deleteInfo
		},'-',
		{
			text:'查询信息',
			iconCls:'zoom',
			scope:this,
			handler:queryInfo
		},'-',
		{
			text:'导入用户',
			iconCls:'excel',
			scope:this,
			handler:importUsers
		}
	];
	var bbar = [
		new Ext.PagingToolbar(
			{
				 store: store,       // grid and PagingToolbar using same store
        		 displayInfo: true,
        		 pageSize: limit,
        		 plugins: new Ext.ux.ProgressBarPager({progBarWidth:400})
			}
		)
	];
	var config = {
		bbar:bbar,
		tbar:tbar,
		sm:sc,
		columns:cm,
		store:store,
		width:400
	};
	UserUnitGridPanel.superclass.constructor.call(this,config);
};
Ext.extend(UserUnitGridPanel,Ext.grid.GridPanel);

UserUnitPanel = function(limit){
	var grid = new UserUnitGridPanel(20);
	var config = {
		items:[grid],
		layout:'fit',
		width:400,
		height:500,
		title:'用户管理',
		iconCls:'zoom',
		id:'UserUnitPanel'
	};
	UserUnitPanel.superclass.constructor.call(this,config);
}
Ext.extend(UserUnitPanel,Ext.Panel);